package com.M10.BE.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.M10.BE.R;
import com.M10.BE.inventory.InveActivity;
import com.fn.useries.UHFApplication;
import com.fn.useries.utils.PreferenceUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends Activity {

    @BindView(R.id.et_user)
    EditText etUser;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.et_url)
    EditText etUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);


        ButterKnife.bind(this);
        etUser.setText(PreferenceUtils.readString(this, "userinfo", "card", ""));
        etPwd.setText(PreferenceUtils.readString(this, "userinfo", "pwd", ""));
        etUser.setSelection(PreferenceUtils.readString(this, "userinfo", "card", "").length());
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
//        UHFApplication.BASE_HEAD=etUrl.getText().toString().trim()+"/cdpfas/app/";
        OkGo.<String>get(UHFApplication.BASE_HEAD + "APPlogin")
                .tag(this)
                .params("username", etUser.getText().toString().trim())
                .params("password", etPwd.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            if (object.optInt("status") == 0) {
                                PreferenceUtils.write(LoginActivity.this, "userinfo", "card", etUser.getText().toString().trim());
                                PreferenceUtils.write(LoginActivity.this, "userinfo", "pwd", etPwd.getText().toString().trim());
                                startActivity(new Intent(LoginActivity.this, InventotyActivity.class));
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, object.optString("msg")
                                        , Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Toast.makeText(LoginActivity.this, "登录失败，请检查网络", Toast.LENGTH_SHORT).show();
                        PreferenceUtils.write(LoginActivity.this, "userinfo", "card", "root");
                        PreferenceUtils.write(LoginActivity.this, "userinfo", "pwd","123456");
                        startActivity(new Intent(LoginActivity.this, InventotyActivity.class));

                    }
                });


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }
}
